package com.greateamd.timetableasy.client.mvc.view.crud.promotion;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.greateamd.timetableasy.client.mvc.model.Model;
import com.greateamd.timetableasy.client.services.promotion.PromotionService;
import com.greateamd.timetableasy.client.services.promotion.PromotionServiceAsync;
import com.greateamd.timetableasy.client.services.util.ServerCall;
import com.greateamd.timetableasy.client.widgets.EntityContainer;
import com.greateamd.timetableasy.client.widgets.campus.CampusListBox;
import com.greateamd.timetableasy.client.widgets.studyperiod.StudyPeriodImplementer;
import com.greateamd.timetableasy.client.widgets.user.UserWithoutPromotionListBox;
import com.greateamd.timetableasy.domain.Course;
import com.greateamd.timetableasy.domain.Promotion;
import com.greateamd.timetableasy.domain.StudyPeriod;
import com.greateamd.timetableasy.domain.User;

public class AddPromotionPage <M extends Model> extends IAddPromotionPage<M>{

	private final PromotionServiceAsync promotionService = GWT.create(PromotionService.class);

	@UiTemplate("AddPromotionPage.ui.xml")
	interface AddPromotionPageUIBinder extends UiBinder<Widget, AddPromotionPage> {}
	private static AddPromotionPageUIBinder uiBinder = GWT.create(AddPromotionPageUIBinder.class);
	
	@UiField TextBox name;
	@UiField Button submitBt;
	//Managers
	@UiField UserWithoutPromotionListBox listOfUsersAvailable;
	@UiField EntityContainer<User> listUsers;
	@UiField Button addUserBt;
	@UiField Button rmUserBt;
	//Cursus
	@UiField StudyPeriodImplementer studyPeriodImpl;
	//Campus
	@UiField CampusListBox listOfCampusAvailable;
	
	/**
	 * Constructor
	 */
	public AddPromotionPage() {
	    initWidget(uiBinder.createAndBindUi(this));
	}
	
	public Widget getWidgetPage() {
		return this;
	}

	@Override
	public void setUser(User user) {
	}


	//---------------------------------------------------------------------
	//-------------------------- UI action

	
	//---------------------------------------------------------------------
	//-------------------------- Handler
	@UiHandler("addUserBt")
	void onAddUserBtClick(ClickEvent e){
		if(listOfUsersAvailable.getSelectedIndex() != -1){
			listUsers.addEntity(listOfUsersAvailable.getSelectedUser());
		}
	}
	@UiHandler("rmUserBt")
	void onRmUserBtClick(ClickEvent e){
		listUsers.removeSelectedEntity();
	}
	@UiHandler("submitBt")
	void onSubmitBtClick(ClickEvent e){
		addPromotion();
	}
	
	//---------------------------------------------------------------------
	//-------------------------- Server call
	
	private void addPromotion() {
		if(!checkFormValidity()){
			Window.alert("Invalid form");
			return;
		}
		new ServerCall<Boolean>() {

			@Override
			protected void callService(AsyncCallback<Boolean> asyncCallback) {
				Promotion toPersist = new Promotion(name.getText(),listOfCampusAvailable.getSelectedCampus());
				for(User user : listUsers.getEntities()){
					toPersist.addUser(user);
					user.setPromotion(toPersist);
				}
				for(StudyPeriod spChoosen :  studyPeriodImpl.getListOfSpChoosen().getEntities()){
					toPersist.addStudyPeriod(spChoosen);
					for(Course c : spChoosen.getCourses()){
						toPersist.addCourse(c);
						c.setPromotion(toPersist);
					}
				}
				promotionService.save(toPersist,asyncCallback);
			}

			@Override
			public void onSuccess(Boolean success) {
				Window.alert("Success " +success);
				//TODO: Could return to a menu
			}
		}.call();
	}

	private boolean checkFormValidity() {
		if(listOfCampusAvailable.getSelectedCampus()==null) return false;
		if(name.getText()=="") return false;
		return true;
	}
}
